import { API_URL } from '../env'
const commonUrl = API_URL
// post请求封装
function postRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this
		var postData = data
		uni.request({
			url: commonUrl + url,
			data: postData,
			method: "POST",
			header: {
				"Content-Type": "application/x-www-form-urlencoded",
				'token': uni.getStorageSync("token"),
			},
			success: function (res) {
				console.log("返回结果===>", res)
				if (res.statusCode == 200) {
					if (res.data.code == 1) {
						resolve(res.data)
					} else if (res.data.code == 401) {
						// #ifdef APP-PLUS
						uni.navigateTo({
							url: "/pages/login/applogin",
						});
						// #endif
						// #ifdef MP-WEIXIN
						uni.navigateTo({
							url: "/pages/login/login",
						});
						// #endif
					} else if (res.data.code == 0) {
						resolve(res.data)
					}
				} else if (res.statusCode == 401) {
					console.log(res)
					uni.clearStorageSync()
					// #ifdef APP-PLUS
					uni.navigateTo({
						url: "/pages/login/applogin",
					});
					// #endif
					// #ifdef MP-WEIXIN
					uni.navigateTo({
						url: "/pages/login/login",
					});
					// #endif
				}
			},
			error: function (e) {
				console.log(e)
				// reject("网络出错")
			}
		})
	})
	return promise
};

// formData格式请求封装
function postFormData(url, data) {
	console.log("formdata接口")
	console.log(url)
	console.log(data)
	var promise = new Promise((resolve, reject) => {
		var that = this
		var postData = data
		uni.uploadFile({
			url: commonUrl + url,
			name: 'file',
			filePath : "-",
			file : ["2"],
			header: {
				'token': uni.getStorageSync("token"),
			},
			formData: data,
			success: function (res) {
				// console.log("返回结果===>", res)

				if (res.statusCode == 200) {
					if (res.data.code == 1) {
						resolve(res.data)
					} else if (res.data.code == 401) {
						// #ifdef APP-PLUS
						uni.navigateTo({
							url: "/pages/login/applogin",
						});
						// #endif
						// #ifdef MP-WEIXIN
						uni.navigateTo({
							url: "/pages/login/login",
						});
						// #endif
					} else if (res.data.code == 0) {
						resolve(res.data)
					}
				} else if (res.statusCode == 401) {
					console.log(res)
					uni.clearStorageSync()
					// uni.showToast({
					// 	title: '网络出错',
					// 	icon: "none"
					// })
					// #ifdef APP-PLUS
					uni.navigateTo({
						url: "/pages/login/applogin",
					});
					// #endif
					// #ifdef MP-WEIXIN
					uni.navigateTo({
						url: "/pages/login/login",
					});
					// #endif
				}
			},
			fail: function (e) {
				console.log(e)
				reject("网络出错")
			}
		})
	})
	return promise
};

// get请求封装
function getRequest(url, data) {	
	var promise = new Promise((resolve, reject) => {
		var that = this
		var postData = data
		uni.request({
			url: commonUrl + url,
			data: postData,
			method: "GET",
			dataType: "json",
			header: {
				"Content-Type": "application/json",
				"token": uni.getStorageSync("token"),
				// "user-token":'15saf1s5dfs5fsd5fsdfs',
			},
			success: function (res) {
				if (res.statusCode == 200) {
					if (res.data.code == 1) {
						resolve(res.data)
					} else if (res.data.code == 401) {
						// #ifdef APP-PLUS
						uni.navigateTo({
							url: "/pages/login/applogin",
						});
						// #endif
						// #ifdef MP-WEIXIN
						uni.navigateTo({
							url: "/pages/login/login",
						});
						// #endif
					} else if (res.data.code == 0) {
						resolve(res.data)
					}
				} else if (res.statusCode == 401) {
					console.log(res)
					uni.clearStorageSync()
					// uni.showToast({
					// 	title: '网络出错',
					// 	icon: "none"
					// })
					// #ifdef APP-PLUS
					uni.navigateTo({
						url: "/pages/login/applogin",
					});
					// #endif
					// #ifdef MP-WEIXIN
					uni.navigateTo({
						url: "/pages/login/login",
					});
					// #endif
				}
			},
			error: function (e) {
				console.log(e)
				reject("网络出错")
			}
		})
	})
	return promise
}

function upfile(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this
		var upData = data
		uni.uploadFile({
			url: commonUrl + url,
			filePath: data,
			name: "file",
			formData: {
				'user': 'test'
			},
			header: {
				// "Content-Type": "application/x-www-form-urlencoded",
				'token': uni.getStorageSync("token"),
			},
			success: (res) => {
				console.log(res, '====>125')
				if (res.statusCode == 200) {
					var json = JSON.parse(res.data)
					if (json.code == 1) {
						resolve(json)
					} else {
						uni.showToast({
							title: "上传数据返回格式错误",
							icon: "none"
						})
					}
				} else if (res.statusCode == 401) {
					console.log(res)
					uni.clearStorageSync()
					// uni.showToast({
					// 	title: '网络出错',
					// 	icon: "none",

					// })
					uni.reLaunch({
						// url:"../../views/login/login?checkState=" + checkState
						url: "/pages/index/login"
					})
				}
			},
			fail: (res) => {
				console.log(res)
				reject("网络出错")
			},
			// complete:() => {
			// 	uni.hideLoading()
			// }
		})
	})
	return promise
}
module.exports = {
	post: postRequest,
	get: getRequest,
	commonUrl: commonUrl,
	upfile: upfile,
	postFormData : postFormData
}
